package com.heima.common.exception;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class ExceptionCatch {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseResult exceptionHandler(Exception e){
        log.error("程序出现异常 ==>{}",e.getMessage());
        e.printStackTrace();
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"程序出现异常");

    }

    @ExceptionHandler(CustomException.class)
    public ResponseResult CustomException(Exception e){
        if (e instanceof CustomException){
            ((CustomException)e).getResponseResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"giao");
    }

}
